//
//  UITableView+TreeView.m
//  lvfj
//
//  Created by lvfeijun on 2021/6/4.
//  Copyright © 2021 Goudu KuaiEx Technology(Shenzhen)Co.,ltd. All rights reserved.
//

#import "UIViewController+TreeView.h"
#import "HQMyFavoriteClassifyCell.h"
#import <RATreeView.h>

static NSString *kHQMyFavoriteClassifyCell = @"HQMyFavoriteClassifyCell";

@interface  UIViewController (TreeView)<RATreeViewDataSource, RATreeViewDelegate,HQMyFavoriteClassifyCellDelegate>

@property (nonatomic, strong) NSMutableArray *data;
@property (nonatomic, weak) RATreeView *treeView;

@end

@implementation UIViewController (TreeView)


- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.view.backgroundColor = [UIColor whiteColor];
    
    self.data = @[@[@[@"1",@"2"],@"3"],@[@[@"4",@"5"],@"6"],@[@"7",@"8"],@"9",@"",@"",@"",@[@"",@""],@[@"",@""],@[@"",@""]];
    
    [self initTableView];
}

#pragma mark - tableView

- (void)initTableView
{
    RATreeView *treeView = [[RATreeView alloc] init];
    treeView.delegate = self;
    treeView.dataSource = self;
    [self.view addSubview:treeView];
    
    //分割线
    treeView.separatorStyle = UITableViewCellSeparatorStyleNone;
    treeView.showsVerticalScrollIndicator = NO;
    self.edgesForExtendedLayout = UIRectEdgeNone;
    
   [treeView registerNib:[UINib nibWithNibName:kHQMyFavoriteClassifyCell bundle:nil] forCellReuseIdentifier:kHQMyFavoriteClassifyCell];
    
    [treeView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.right.bottom.mas_equalTo(self.view);
    }];
    
    self.treeView = treeView;
    
    UITableView *tableview = [treeView valueForKeyPath:@"tableView"];
    if ([tableview isKindOfClass:[UITableView class]]) {
        @weakify(self)
        tableview.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
            @strongify(self)
            [self refreshData];
        }];
        tableview.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingBlock:^{
            @strongify(self)
            [self loadMoreData];
        }];
    }
}



- (void)refreshData
{
    DDLogDebug(@"refreshData");
//    [self.tableView.mj_header endRefreshing];
}

- (void)loadMoreData
{
    DDLogDebug(@"loadMoreData");
//    [self.tableView.mj_footer endRefreshing];
}

- (BOOL)canOpen:(id)item
{
    return [item isKindOfClass:[NSArray class]]||
    [item isKindOfClass:[NSDictionary class]];
}

- (void)expandWithCell:(HQMyFavoriteClassifyCell *)cell item:(NSArray *)item
{
    if ([self canOpen:item]) {
        if (![self.treeView isCellExpanded:cell]) {
            [self treeView:self.treeView willExpandRowForItem:item];
            [self.treeView expandRowForItem:item expandChildren:NO withRowAnimation:RATreeViewRowAnimationFade];
        } else {
            [self treeView:self.treeView willCollapseRowForItem:item];
            [self.treeView collapseRowForItem:item collapseChildren:NO withRowAnimation:RATreeViewRowAnimationFade];
        }

        
        switch (cell.midType) {
            case HQChapterExerciseCellMidTypePlus:
                cell.midType = HQChapterExerciseCellMidTypeMinus;
                break;
                
            case HQChapterExerciseCellMidTypeSubPlus:
                cell.midType = HQChapterExerciseCellMidTypeSubMinus;
                break;
                
            case HQChapterExerciseCellMidTypeMinus:
                cell.midType = HQChapterExerciseCellMidTypePlus;
                break;
                
            case HQChapterExerciseCellMidTypeSubMinus:
                cell.midType = HQChapterExerciseCellMidTypeSubPlus;
                break;
                
            default:
                break;
        }
        
    }
}
- (void)clickWithCell:(HQMyFavoriteClassifyCell *)cell item:(NSArray *)item
{
    
}


#pragma mark - RATreeView

- (CGFloat)treeView:(RATreeView *)treeView heightForRowForItem:(id)item
{
    return 90.0;
}

- (NSInteger)treeView:(RATreeView *)treeView indentationLevelForRowForItem:(id)item
{
    return 3;
}

- (BOOL)treeView:(RATreeView *)treeView shouldExpandRowForItem:(id)item
{
    return YES;
}

- (void)treeView:(RATreeView *)treeView didSelectRowForItem:(id)item
{
    NSInteger level = [treeView levelForCellForItem:item];
    [treeView deselectRowForItem:item animated:YES];
    DDLogDebug(@"点击的是第%ld层",level);
}

- (UITableViewCell *)treeView:(RATreeView *)treeView cellForItem:(id)item
{
    HQMyFavoriteClassifyCell *cell = [treeView dequeueReusableCellWithIdentifier:kHQMyFavoriteClassifyCell];
    cell.item = item;
    cell.delegate = self;
    
    NSInteger numberOfChildren = [self canOpen:item]&&[item count];
    NSInteger level = [treeView levelForCellForItem:item];
    if (numberOfChildren) {
        if (level == 0) {
            if ([treeView isCellForItemExpanded:item]) {
                cell.midType = HQChapterExerciseCellMidTypeMinus;
            } else {
                cell.midType = HQChapterExerciseCellMidTypePlus;
            }
        } else if (level == 1) {
            if ([treeView isCellForItemExpanded:item]) {
                cell.midType = HQChapterExerciseCellMidTypeSubMinus;
            } else {
                cell.midType = HQChapterExerciseCellMidTypeSubPlus;
            }

        } else {
            cell.midType = HQChapterExerciseCellMidTypeBlank;
        }
        
    } else {
        if (level != 0) {
            cell.midType = HQChapterExerciseCellMidTypeBlank;
        } else {
            if ([treeView isCellForItemExpanded:item]) {
                cell.midType = HQChapterExerciseCellMidTypeMinus;
            } else {
                cell.midType = HQChapterExerciseCellMidTypePlus;
            }
        }
    }
    return cell;
}

- (NSInteger)treeView:(RATreeView *)treeView numberOfChildrenOfItem:(id)item
{
    if (!item) {
        return self.data.count;
    } else if ([self canOpen:item]) {
        return  [item count];
    }
    return 0;
}

- (id)treeView:(RATreeView *)treeView child:(NSInteger)index ofItem:(id)item
{
    if (!item) {
        return self.data[index];
    } else {
        return item[index];
    }
}

- (BOOL)treeView:(RATreeView *)treeView canEditRowForItem:(id)item
{
    if (!item) {
        return YES;
    } else if ([self canOpen:item]) {
        return  YES;
    }
    return NO;
}

- (void)treeView:(RATreeView *)treeView willExpandRowForItem:(id)item
{
    
}

- (void)treeView:(RATreeView *)treeView willCollapseRowForItem:(id)item
{
    
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
